<!--
 * Copyright (c) 2013-Now http://jeesite.com All rights reserved.
 * No deletion without permission, or be held responsible to law.
 * @author YDZ
-->
<template>
  <BasicDrawer
    v-bind="$attrs"
    :showFooter="true"
    :okAuth="'core:bas:baseClda:edit'"
    @register="registerDrawer"
    @ok="handleSubmit"
    width="60%"
  >
    <template #title>
      <Icon :icon="getTitle.icon" class="m-1 pr-1" />
      <span> {{ getTitle.value }} </span>
    </template>
    <BasicForm @register="registerForm" />
  </BasicDrawer>
</template>
<script lang="ts" setup name="ViewsCoreBasBaseCldaForm">
  import { ref, unref, computed } from 'vue';
  import { useI18n } from '/@/hooks/web/useI18n';
  import { useMessage } from '/@/hooks/web/useMessage';
  import { router } from '/@/router';
  import { Icon } from '/@/components/Icon';
  import { BasicForm, FormSchema, useForm } from '/@/components/Form';
  import { BasicDrawer, useDrawerInner } from '/@/components/Drawer';
  import { BaseClda, baseCldaSave, baseCldaForm } from '/@/api/core/bas/baseClda';

  const emit = defineEmits(['success', 'register']);

  const { t } = useI18n('core.bas.baseClda');
  const { showMessage } = useMessage();
  const { meta } = unref(router.currentRoute);
  const record = ref<BaseClda>({} as BaseClda);

  const getTitle = computed(() => ({
    icon: meta.icon || 'i-ant-design:book-outlined',
    value: record.value.isNewRecord ? t('新增车辆档案') : t('编辑车辆档案'),
  }));

  const inputFormSchemas: FormSchema[] = [
    {
      label: t('车牌号码'),
      field: 'cldaCphm',
      component: 'Input',
      componentProps: {
        maxlength: 100,
      },
      required: true,
    },
    {
      label: t('品牌型号'),
      field: 'cldaPpxh',
      component: 'Input',
      componentProps: {
        maxlength: 100,
      },
      required: true,
    },
    {
      label: t('车辆类型'),
      field: 'cldaCllx',
      component: 'Input',
      componentProps: {
        maxlength: 100,
      },
      required: true,
    },
    {
      label: t('使用性值'),
      field: 'cldaSyxz',
      component: 'Input',
      componentProps: {
        maxlength: 100,
      },
      required: true,
    },
    {
      label: t('车辆所有人'),
      field: 'cldaClsyr',
      component: 'Input',
      componentProps: {
        maxlength: 100,
      },
      required: true,
    },
    {
      label: t('座位数'),
      field: 'cldaZws',
      component: 'Input',
      componentProps: {
        maxlength: 8,
      },
      rules: [{ pattern: /^\d+$/, message: t('请输入一个正整数') }],
    },
    {
      label: t('默认驾驶员'),
      field: 'cldaMrjsy',
      component: 'Input',
      componentProps: {
        maxlength: 100,
      },
    },
    {
      label: t('联系电话'),
      field: 'cldaLxdh',
      component: 'Input',
      componentProps: {
        maxlength: 100,
      },
    },
    {
      label: t('车辆状态'),
      field: 'cldaClzt',
      component: 'Input',
      componentProps: {
        maxlength: 100,
      },
    },
    {
      label: t('发动机号码'),
      field: 'cldaFdjhm',
      component: 'Input',
      componentProps: {
        maxlength: 100,
      },
    },
    {
      label: t('车辆识别代号'),
      field: 'cldaClsbdh',
      component: 'Input',
      componentProps: {
        maxlength: 100,
      },
    },
    {
      label: t('新能源汽车'),
      field: 'cldaXnyqc',
      component: 'Input',
      componentProps: {
        maxlength: 100,
      },
    },
    {
      label: t('车体颜色'),
      field: 'cldaCtys',
      component: 'Input',
      componentProps: {
        maxlength: 100,
      },
    },
    {
      label: t('车辆配置'),
      field: 'cldaClpz',
      component: 'Input',
      componentProps: {
        maxlength: 100,
      },
    },
    {
      label: t('核定载人数'),
      field: 'cldaHdzrs',
      component: 'Input',
      componentProps: {
        maxlength: 8,
      },
      rules: [{ pattern: /^\d+$/, message: t('请输入一个正整数') }],
    },
    {
      label: t('整备质量'),
      field: 'cldaZbzl',
      component: 'Input',
      componentProps: {
        maxlength: 8,
      },
      rules: [{ pattern: /^\d+$/, message: t('请输入一个正整数') }],
    },
    {
      label: t('总质量'),
      field: 'cldaZzl',
      component: 'Input',
      componentProps: {
        maxlength: 8,
      },
      rules: [{ pattern: /^\d+$/, message: t('请输入一个正整数') }],
    },
    {
      label: t('变速器型式'),
      field: 'cldaBsqxs',
      component: 'Input',
      componentProps: {
        maxlength: 100,
      },
    },
    {
      label: t('发动机排量'),
      field: 'cldaFdjpl',
      component: 'Input',
      componentProps: {
        maxlength: 100,
      },
    },
    {
      label: t('油箱容积'),
      field: 'cldaYxrj',
      component: 'Input',
      componentProps: {
        maxlength: 8,
      },
      rules: [{ pattern: /^\d+$/, message: t('请输入一个正整数') }],
    },
    {
      label: t('保养周期'),
      field: 'cldaByzq',
      component: 'Input',
      componentProps: {
        maxlength: 8,
      },
      rules: [{ pattern: /^\d+$/, message: t('请输入一个正整数') }],
    },
    {
      label: t('销售单位'),
      field: 'cldaXsdw',
      component: 'Input',
      componentProps: {
        maxlength: 100,
      },
    },
    {
      label: t('购车时间'),
      field: 'cldaGcsj',
      component: 'DatePicker',
      componentProps: {
        format: 'YYYY-MM-DD HH:mm',
        showTime: { format: 'HH:mm' },
      },
    },
    {
      label: t('购车价格'),
      field: 'cldaGcjg',
      component: 'Input',
      componentProps: {
        maxlength: 8,
      },
      rules: [{ pattern: /^(?:-?\d+|-?\d{1,3}(?:,\d{3})+)?(?:\.\d+)?$/, message: t('请输入一个数值') }],
    },
    {
      label: t('注册登记日期'),
      field: 'cldaZcdjrq',
      component: 'DatePicker',
      componentProps: {
        format: 'YYYY-MM-DD HH:mm',
        showTime: { format: 'HH:mm' },
      },
    },
    {
      label: t('发证日期'),
      field: 'cldaFzrq',
      component: 'DatePicker',
      componentProps: {
        format: 'YYYY-MM-DD HH:mm',
        showTime: { format: 'HH:mm' },
      },
    },
    {
      label: t('年检日期'),
      field: 'cldaNjrq',
      component: 'DatePicker',
      componentProps: {
        format: 'YYYY-MM-DD HH:mm',
        showTime: { format: 'HH:mm' },
      },
    },
    {
      label: t('保险到期'),
      field: 'cldaBxdq',
      component: 'DatePicker',
      componentProps: {
        format: 'YYYY-MM-DD HH:mm',
        showTime: { format: 'HH:mm' },
      },
    },
    {
      label: t('限行标志'),
      field: 'cldaXxbz',
      component: 'Input',
      componentProps: {
        maxlength: 8,
      },
      rules: [{ pattern: /^\d+$/, message: t('请输入一个正整数') }],
    },
    {
      label: t('录入人员'),
      field: 'cldaLrry',
      component: 'Input',
      componentProps: {
        maxlength: 100,
      },
    },
    {
      label: t('录入日期'),
      field: 'cldaLrrq',
      component: 'DatePicker',
      componentProps: {
        format: 'YYYY-MM-DD HH:mm',
        showTime: { format: 'HH:mm' },
      },
    },
    {
      label: t('修改人员'),
      field: 'cldaXgry',
      component: 'Input',
      componentProps: {
        maxlength: 100,
      },
    },
    {
      label: t('修改日期'),
      field: 'cldaXgrq',
      component: 'DatePicker',
      componentProps: {
        format: 'YYYY-MM-DD HH:mm',
        showTime: { format: 'HH:mm' },
      },
    },
    {
      label: t('图片上传'),
      field: 'dataMap',
      component: 'Upload',
      componentProps: {
        loadTime: computed(() => record.value.__t),
        bizKey: computed(() => record.value.id),
        bizType: 'baseClda_image',
        uploadType: 'image',
      },
      colProps: { lg: 24, md: 24 },
    },
    {
      label: t('附件上传'),
      field: 'dataMap',
      component: 'Upload',
      componentProps: {
        loadTime: computed(() => record.value.__t),
        bizKey: computed(() => record.value.id),
        bizType: 'baseClda_file',
        uploadType: 'all',
      },
      colProps: { lg: 24, md: 24 },
    },
  ];

  const [registerForm, { resetFields, setFieldsValue, validate }] = useForm({
    labelWidth: 120,
    schemas: inputFormSchemas,
    baseColProps: { lg: 8, md: 24 },
  });

  const [registerDrawer, { setDrawerProps, closeDrawer }] = useDrawerInner(async (data) => {
    setDrawerProps({ loading: true });
    await resetFields();
    const res = await baseCldaForm(data);
    record.value = (res.baseClda || {}) as BaseClda;
    record.value.__t = new Date().getTime();
    setFieldsValue(record.value);
    setDrawerProps({ loading: false });
  });

  async function handleSubmit() {
    try {
      const data = await validate();
      setDrawerProps({ confirmLoading: true });
      const params: any = {
        isNewRecord: record.value.isNewRecord,
        id: record.value.id,
      };
      // console.log('submit', params, data, record);
      const res = await baseCldaSave(params, data);
      showMessage(res.message);
      setTimeout(closeDrawer);
      emit('success', data);
    } catch (error: any) {
      if (error && error.errorFields) {
        showMessage(t('common.validateError'));
      }
      console.log('error', error);
    } finally {
      setDrawerProps({ confirmLoading: false });
    }
  }
</script>
